package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.*;
import com.example.campusforumapi.service.ActivityService;
import com.example.campusforumapi.service.ArticleService;
import com.example.campusforumapi.util.AliOSSUtils;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activities")
public class ActivityController extends BaseController{
    @Autowired
    private ActivityService activityService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    //    查询所有文章标签记录
    @GetMapping("/getCategories")
    public Result getCategories() {
        List<ActivityTag> categoryList = activityService.getCategories();
        return Result.success(categoryList);
    }
    //    查询所有学院信息记录
    @GetMapping("/getDepartments")
    public Result getDepartments() {
        List<Department> departmentList = activityService.getDepartments();
        return Result.success(departmentList);
    }
    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer activityId, Integer userId, String title, Integer tagId, Integer departmentId,Integer status, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){

        //        查询并封装分页结果集
        PageResult activityPage = activityService.getActivityPage(page,pageSize,activityId,userId,title,tagId,departmentId,status,startTime,endTime);

        return Result.success(activityPage);
    }

    //    删除一条或多条记录
    @DeleteMapping ("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        activityService.delActivity(ids);
        return Result.success();
    }

    //    新增一条记录
    @PostMapping ("/insert")
    public Result insert(@RequestBody Activity activity){
        if(StringUtils.isEmpty(activity.getTitle()) || StringUtils.isEmpty(activity.getUserId())){
            return Result.error("标题及作者id不能为空");
        }
        else if (StringUtils.isEmpty(activity.getDescription())) {
            return Result.error("活动描述不能为空");
        }else if(StringUtils.isEmpty(activity.getDepartmentId())){
            return Result.error("活动院系不能为空");
        }else if(activityService.insertActivity(activity) > 0){
            return Result.success("插入成功");
        }else{
            return Result.error("插入异常");
        }

    }

    /*
     * 文章附件文件上传 获取阿里云url地址
     * */
    @PostMapping("/attachmentUpload")
    public Result attachmentUpload(@RequestParam("file") MultipartFile attachment) throws IOException {
        log.info("文件上传，文件名：{ }",attachment.getOriginalFilename());
        String url = aliOSSUtils.upload(attachment);
        return Result.success(url);

    }
    //    修改一条记录
    @PutMapping("/update")
    public Result update(@RequestBody Activity activity){
        if(activityService.updActivity(activity) > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败，该活动不存在");
        }

    }

    /*
     * 按条件  分页查询 审核状态的活动查询
     * */
    @RequestMapping(value = "/AuditList", method = RequestMethod.GET)
    public Result AuditList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer activityId){

        //        查询并封装分页结果集
        PageResult auditActivityPage = activityService.getAuditActivityPage(page,pageSize,activityId);

        return Result.success(auditActivityPage);
    }
    //    活动审核通过时相应操作
    @PutMapping("/approve")
    public Result approve(@RequestBody Activity activity){
        log.info("{}{}{}",activity.getStartTime(),activity.getActivityId(),activity.getEndTime());
        if(activityService.updAproveActivity(activity) > 0){
            return Result.success("审核成功");
        }else{
            return Result.error("审核失败");
        }

    }
    //    活动审核通过时相应操作
    @PutMapping("/reject")
    public Result reject(@RequestBody Activity activity){
        log.info("{}",activity.getFeedback());
        if(activityService.updRejectActivity(activity) > 0){
            return Result.success("操作成功");
        }else{
            return Result.error("操作失败");
        }

    }
}
